package common;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import util.KylinUtils;


public class KylinManager {
	
    private final static Logger LOGGER = LogManager.getLogger(KylinManager.class);

	
	/**
	 * 获取在线用户数
	 * @param startDate 开始时间
	 * @param endDate 结束时间
	 * @param flag 时间正反序标志
	 * @param pageSize 分页大小
	 * @param page 第几页
	 * @return
	 */
	public static List<Map<String, Object>> getUserLoginList(String startDate, String endDate, String flag,
			int pageSize, int page) throws SQLException{
		

		List<Map<String, Object>> list = null;
		try{
			String sql = "select map";
			List<Object> params = new ArrayList<Object>();
			KylinUtils.selectMapListDB(sql, params);
		}catch(Exception e){
			
		}finally{
			
		}

		return list;
	}

	/**
	 * 获取每天登录平台的活跃用户数
	 * @param flag 查询标志(1--周；2--月)
	 * @return
	 */
	public static int getActiveUserCount(int flag) throws Exception{
		int count = 0;
		String sql = "";
		List<Object> paramList = new ArrayList<Object>();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		try{
			if(1 == flag){
				Calendar c = Calendar.getInstance();
				//默认是从周日开始，下面改为周一开始
				int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
				if (day_of_week == 0){
					day_of_week = 7;
				}
				c.add(Calendar.DATE, -day_of_week + 1);
				//本周周一时间
				String monday = format.format(c.getTime());
				paramList.add(Integer.parseInt(monday));
				paramList.add(new Long(day_of_week));
			}else if(2 == flag){
		        // 获取前月的第一天  
		        Calendar cale = Calendar.getInstance();  
		        cale.add(Calendar.MONTH, 0);  
		        cale.set(Calendar.DAY_OF_MONTH, 1);  
		        String firstday = format.format(cale.getTime()); 
		        SimpleDateFormat format1 = new SimpleDateFormat("dd"); 
		        String day = format1.format(new Date());
				paramList.add(Integer.parseInt(firstday));
				paramList.add(Long.parseLong(day));
			}else{
				LOGGER.error("查询类别有误，请确定查询参数为按月或者按周");
				throw new Exception("查询类别有误，请确定查询参数为按月或者按周");
			}
			sql = "select count(1) from (select uid from VIEW_LOG_USER_LOGIN where create_date>=? group by uid having count(distinct create_date)>=?)t;";
			count = KylinUtils.queryCount(sql, paramList);
		}catch(Exception e){
			LOGGER.error(e.getMessage());
			throw e;
		}
		return count;
	}
	
	
	/**
	 * 获取【本月每周/本周】登录平台3次以上的活跃用户数
	 * @param flag 查询标志(1--周；2--月)
	 * @return
	 */
	public static int getActiveUserThreeCount(int flag) throws Exception{
		int count = 0;
		String sql = "";
		List<Object> paramList = new ArrayList<Object>();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		try{
			if (1 == flag) {
				//本周周一时间
				String monday = getMondayDate();
				sql = "select count(1) from (select uid from VIEW_LOG_USER_LOGIN where create_date>=? group by uid,week having count(1)>=3)t";
				paramList.add(Integer.parseInt(monday));
			} else if (2 == flag) {
				String firstday = getMonthFirstday();
				Calendar c = Calendar.getInstance();
				c.setTime(new Date());
				int weekOfMonth = c.get(Calendar.WEEK_OF_MONTH);
				paramList.add(Integer.parseInt(firstday));
				paramList.add(new Long(weekOfMonth));
				sql = "select count(1) from(select uid from (select uid,count(1),week from VIEW_LOG_USER_LOGIN where create_date>=? group by uid,week having count(1)>=3) tmp group by uid having count(1)=?)t;";
			} else {
				LOGGER.error("查询类别有误，请确定查询参数为按月或者按周");
				throw new Exception("查询类别有误，请确定查询参数为按月或者按周");
			}
			count = KylinUtils.queryCount(sql, paramList);
		}catch(Exception e){
			LOGGER.error(e.getMessage());
			throw e;
		}
		return count;
	}
	
	/**
	 * 获取用户【本月每周/本周】登录平台总数、成功次数、失败次数统计。
	 * @param flag 查询标志(1--周；2--月)
	 * @return
	 */
	public static Map<String,Object> getUserLogStatistics(int flag) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		int count = 0;
		String sql = "";
		List<Object> paramList = new ArrayList<Object>();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		try{
			if (1 == flag) {
				//本周周一时间
				String monday = getMondayDate();
				paramList.add(Integer.parseInt(monday));
			} else if (2 == flag) {
				String firstday = getMonthFirstday();
				paramList.add(Integer.parseInt(firstday));
			} else {
				LOGGER.error("查询类别有误，请确定查询参数为按月或者按周");
				throw new Exception("查询类别有误，请确定查询参数为按月或者按周");
			}
			//总登录次数
			sql = "select count(1) from VIEW_LOG_USER_LOGIN where create_date>=?;";
			count = KylinUtils.queryCount(sql, paramList);
			map.put("total", count);
			
			//登录成功次数
			sql = "select count(1) from VIEW_LOG_USER_LOGIN where create_date>=? and success=0;";
			count = KylinUtils.queryCount(sql, paramList);
			map.put("success", count);

			//登录失败次数
			sql = "select count(1) from VIEW_LOG_USER_LOGIN where create_date>=? and success=1;";
			count = KylinUtils.queryCount(sql, paramList);
			map.put("fail", count);
			
		}catch(Exception e){
			LOGGER.error(e.getMessage());
			throw e;
		}
		return map;
	}
	
	/**
	 * 获取用户【本月/本周】访问模块统计。
	 * @param flag 查询标志(1--周；2--月)
	 * @return
	 * @throws Exception
	 */
	public static Map<String, Object> getModuleStatistic(int flag) throws Exception{
		List<Map<String, Object>> list = null;
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<Object> paramList = new ArrayList<Object>();
		if (1 == flag) {
            String monday = getMondayDate();
			paramList.add(Integer.parseInt(monday));
		} else if (2 == flag) {
			String firstday = getMonthFirstday();
			paramList.add(Integer.parseInt(firstday));
		}else{
			LOGGER.error("查询类别有误，请确定查询参数为按月或者按周");
			throw new Exception("查询类别有误，请确定查询参数为按月或者按周");
		}
		String sql = "select log_module,count(1) number from viewt2 where create_date>=? group by log_module";
		list = KylinUtils.selectMapListDB(sql, paramList);
		if(list !=null && list.size() > 0 ){
			for(int i = 0; i < list.size(); i++){
				Map<String, Object> map = (Map<String, Object>)list.get(i);
				resultMap.put(map.get("LOG_MODULE").toString(), map.get("NUMBER"));
			}
		}
		return resultMap;
	}

	//获取本周周一的日期
	private static String getMondayDate(){
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Calendar c = Calendar.getInstance();
        //默认是从周日开始，下面改为周一开始
        int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0){
            day_of_week = 7;
        }
        c.add(Calendar.DATE, -day_of_week + 1);
        //本周周一时间
        return format.format(c.getTime());
    }
    //获取本月第一天日期
    private static String getMonthFirstday(){
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        return format.format(cale.getTime());
    }

    public static void main(String args[]) throws Exception{
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat format1 = new SimpleDateFormat("dd");
        String firstday, lastday;
        // 获取前月的第一天
       /* Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        firstday = format.format(cale.getTime());
        System.out.println( format1.format(new Date()));
        System.out.println(firstday);
        Calendar calendar = new GregorianCalendar();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setMinimalDaysInFirstWeek(7);
        calendar.setTime(new Date());
        System.out.println(calendar.get(Calendar.DAY_OF_WEEK));
        System.out.println(calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
        System.out.println(KylinManager.getActiveUserCount(1));
        System.out.println(KylinManager.getActiveUserThreeCount(2));

        Calendar c = Calendar.getInstance();
		int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (day_of_week == 0)
		day_of_week = 7;
		c.add(Calendar.DATE, -day_of_week + 1);
		System.out.println(c.getTime());*/
       // KylinManager.getModuleStatistic(1);
        KylinManager.getActiveUserCount(2);
    }
}
